local addon, ns = ...
local C = ns.shared.color
local func = ns.shared
local L = ns.L

local tooltip = nil

local friends_onevent = function (self)
	local numFriends, friendsOnline = GetNumFriends()
	local numBNFriends, numOnlineBNFriends = BNGetNumFriends()
	local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["Friends"])

	dataobj.text = friendsOnline + numOnlineBNFriends .. "/" .. numFriends + numBNFriends
end


local friends_events = {
	"PLAYER_LOGIN",
	"FRIENDLIST_UPDATE",
	"PLAYER_ENTERING_WORLD",
	"BN_FRIEND_ACCOUNT_ONLINE",
	"BN_FRIEND_ACCOUNT_OFFLINE",
}

local function drawFriendsTip()
	local line, column
	tooltip:AddHeader(C.dkyellow(L["Friends"]))

	local numBNFriends, numOnlineBNFriends = BNGetNumFriends()
	if numOnlineBNFriends > 0 then
		tooltip:AddLine(C.ltyellow(L["Real ID"]), C.ltyellow(L["Character"]), C.ltyellow(L["Game : Realm"]), C.ltyellow(L["Notes"]))	
		tooltip:AddSeparator()

		for i = 1, numBNFriends, 1 do
			--local presenceID, givenName, surname, toonName, toonID, client, isOnline, lastOnline, isAFK, isDND, broadcast, notes = BNGetFriendInfo(i)
			local presenceID, presenceName, battleTag, isBattleTagPresence, toonName, toonID, game, isOnline, lastOnline, isAFK, isDND, broadcastText, noteText, isFriend, broadcastTime, canSoR = BNGetFriendInfo(i)

			local _, _, _, realm, _, faction, race, class, guild, zone, level, gameText = BNGetToonInfo(toonID or 0);

			if isOnline then
				--line, column = tooltip:AddLine(C.ltblue(string.format("%s %s", givenName, surname)), C.className(toonName, class), C.white(string.format("%s : %s", game, realm)), C.grey(notes or " ") )
				line, column = tooltip:AddLine(C.ltblue(presenceName), C.className(toonName, class), C.white(string.format("%s : %s", game, realm)), C.grey(notes or " ") )
				local name = presenceID and presenceName
		--SetItemRef( "player:"..name, format( "|Hplayer:%1$s|h[%1$s]|h", name ), "LeftButton" )
				tooltip:SetLineScript(line, "OnMouseUp", function(self) if IsAltKeyDown() then InviteUnit(toonName) else SetItemRef("player:"..name, "|Hplayer:"..name.."|h["..name.."|h", "LeftButton") end end, n)	
				tooltip:SetLineScript(line, "OnEnter", function(self) tooltip:SetLineColor(line, 1,192/255, 90/255, 0.3) end )
				tooltip:SetLineScript(line, "OnLeave", function(self) tooltip:SetLineColor(line, 0,0,0,0) end)
			end
		end
		tooltip:AddLine(" ")
		line, column = nil, nil
	end


	local numFriends, friendsOnline = GetNumFriends()

	if friendsOnline > 0 then 
		tooltip:AddLine(C.ltyellow(L["Character"]), C.ltyellow(L["Level"]), C.ltyellow(L["Zone"]), C.ltyellow(L["Notes"]))
		tooltip:AddSeparator()
	
		for i = 1, numFriends do
			local n, l, c, a, o, s, no = GetFriendInfo(i)
			if n and o then
				if s == 0 then
					s = ""
				elseif s== 1 then
					s = "<AFK>"
				elseif s == 2 then
					s = "<DND>"
				end

				local character = string.format("%s %s", s or "", n)
				line, column = tooltip:AddLine(C.className(character, strupper(c)), l, a or "", no or "")
				tooltip:SetLineScript(line, "OnMouseUp", function(self) if IsAltKeyDown() then InviteUnit(n) else SetItemRef("player:"..n, "|Hplayer:"..n.."|h["..n.."|h", "LeftButton") end end, n)			
				tooltip:SetLineScript(line, "OnEnter", function(self) tooltip:SetLineColor(line, 1,192/255, 90/255, 0.3) end )
				tooltip:SetLineScript(line, "OnLeave", function(self) tooltip:SetLineColor(line, 0,0,0,0) end)
			end
		end
	else
		if numOnlineBNFriends == 0 then 
			tooltip:AddLine(L["No Friends Online."])
		end
	end
	local line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["Hint: Left-click to open the friends roster."]), nil, nil, 4)
	line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["Click a friend to whisper."]), nil, nil, 4)
	line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["Alt + Click a friend to invite."]), nil, nil, 4)

	line, column = nil, nil
end



local friends_init = function(obj)
	if not obj then return end

	obj = obj.obj

	obj.OnLeave = function(self)
		if tooltip then
			if MouseIsOver(tooltip) then
					tooltip:SetScript('OnLeave', func.hideTooltip)
			else
					func.hideTooltip(tooltip)
			end
		end
	end

	obj.OnEnter = function(self)
		tooltip = LibQTip:Acquire("FriendsTT", 4 , "LEFT", "CENTER", "CENTER", "LEFT") 
		drawFriendsTip()
		func.createTooltip(self,tooltip)
	end

end


local friends_onclick = function()
	ToggleFriendsFrame(1)
end


Broker_Everything:Register(L["Friends"],
	--GetItemIcon(22167),
	"Interface\\Addons\\Broker_Everything\\media\\friends",
	friends_init,
	friends_onevent,
	friends_events,
	friends_onclick,
	0, -- friends_tooltip,
	nil,
	nil)

